{% extends "base.html" %}

{% block content %}
<div class="container mt-5">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">
                    <h4 class="mb-0">编辑论文信息</h4>
                </div>
                <div class="card-body">
                    <form method="POST" enctype="multipart/form-data">
                        {{ form.hidden_tag() }}
                        <div class="mb-3">
                            {{ form.title.label(class="form-label") }}
                            {{ form.title(class="form-control") }}
                            {% for error in form.title.errors %}
                            <span class="text-danger">{{ error }}</span>
                            {% endfor %}
                        </div>
                        <div class="mb-3">
                            {{ form.description.label(class="form-label") }}
                            {{ form.description(class="form-control", rows="4") }}
                            {% for error in form.description.errors %}
                            <span class="text-danger">{{ error }}</span>
                            {% endfor %}
                        </div>
                        <div class="mb-3">
                            {{ form.programming_language.label(class="form-label") }}
                            {{ form.programming_language(class="form-control") }}
                            {% for error in form.programming_language.errors %}
                            <span class="text-danger">{{ error }}</span>
                            {% endfor %}
                        </div>
                        <div class="mb-3">
                            {{ form.price.label(class="form-label") }}
                            <div class="input-group">
                                {{ form.price(class="form-control", type="number", min="500", max="2050", step="100", value="500") }}
                                <span class="input-group-text">元</span>
                            </div>
                            {% for error in form.price.errors %}
                            <span class="text-danger">{{ error }}</span>
                            {% endfor %}
                            <small class="form-text text-muted">价格范围：500-2050元，每次增减100元</small>
                        </div>
                        <div class="mb-3">
                            {{ form.contact_info.label(class="form-label") }}
                            {{ form.contact_info(class="form-control") }}
                            {% for error in form.contact_info.errors %}
                            <span class="text-danger">{{ error }}</span>
                            {% endfor %}
                        </div>
                        <div class="mb-3">
                            {{ form.deadline.label(class="form-label") }}
                            {{ form.deadline(class="form-control", type="date") }}
                            {% for error in form.deadline.errors %}
                            <span class="text-danger">{{ error }}</span>
                            {% endfor %}
                        </div>
                        <div class="mb-3 form-check">
                            {{ form.need_environment(class="form-check-input") }}
                            {{ form.need_environment.label(class="form-check-label") }}
                        </div>
                        <div class="mb-3">
                            {{ form.proposal_file.label(class="form-label") }}
                            {{ form.proposal_file(class="form-control") }}
                            {% if thesis.proposal_file %}
                            <small class="form-text text-muted">
                                当前文件：{{ thesis.proposal_file }}
                                <a href="{{ url_for('main.download_proposal', filename=thesis.proposal_file) }}" class="btn btn-sm btn-link">
                                    <i class="fas fa-download"></i> 下载
                                </a>
                            </small>
                            {% endif %}
                            {% for error in form.proposal_file.errors %}
                            <span class="text-danger">{{ error }}</span>
                            {% endfor %}
                            <small class="form-text text-muted">支持的文件格式：PDF、Word文档（doc/docx）</small>
                        </div>
                        <div class="d-flex justify-content-between">
                            <a href="{{ url_for('main.thesis_detail', id=thesis.id) }}" class="btn btn-secondary">取消</a>
                            {{ form.submit(class="btn btn-primary") }}
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

{% block scripts %}
<script>
// 确保价格输入符合步进值
document.querySelector('input[type="number"]').addEventListener('change', function(e) {
    let value = parseInt(this.value);
    if (value < 500) this.value = 500;
    if (value > 2050) this.value = 2050;
    // 确保值是100的倍数
    let basePrice = Math.round(value / 100) * 100;
    // 如果需要环境配置，添加50元
    if (document.getElementById('need_environment').checked) {
        this.value = basePrice + 50;
    } else {
        this.value = basePrice;
    }
});

// 处理环境配置选项改变
document.getElementById('need_environment').addEventListener('change', function(e) {
    let priceInput = document.querySelector('input[type="number"]');
    let currentPrice = parseInt(priceInput.value);
    
    if (this.checked) {
        // 选中时加50
        priceInput.value = currentPrice + 50;
    } else {
        // 取消选中时减50
        priceInput.value = currentPrice - 50;
    }
});
</script>
{% endblock %}

{% endblock %} 